<%
layout("layout.html",{title:'用户管理', first: '用户', second: '用户列表', menu: '用户'}){
%>
<div class="dataTables_wrapper form-inline dt-bootstrap">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <h3 class="box-title">用户列表</h3>
                </div>
                <div class="box-body">
                    <div>
                        <button class="btn btn-primary" data-toggle="modal" data-target="#createModal">新建</button>
                        <div class="modal fade" id="createModal" tabindex="-1" role="dialog">
                            <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                        <h4 class="modal-title">新建用户</h4>
                                    </div>
                                    <div class="modal-body">
                                        <form role="form">
                                            <div class="form-group">
                                                <label for="nicknameInput">昵称</label>
                                                <input type="text" class="form-control" id="nicknameInput" placeholder="请输入昵称" />
                                            </div>
                                            <div class="form-group">
                                                <label for="phoneInput">手机</label>
                                                <input type="tel" class="form-control" id="phoneInput" placeholder="请输入手机号码" />
                                            </div>
                                            <div class="form-group">
                                                <label for="passwordInput">密码</label>
                                                <input type="password" class="form-control" id="passwordInput" placeholder="请输入密码" />
                                            </div>
                                            <div class="form-group">
                                                <label for="moneyInput">最低金额</label>
                                                <input type="number" class="form-control" id="moneyInput" placeholder="请输入最低金额" />
                                            </div>
                                        </form>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                        <button type="button" class="btn btn-primary" id="saveButton">保存</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <table class="table">
                        <thead>
                        <tr>
                            <th>id</th>
                            <th>昵称</th>
                            <th>手机</th>
                            <th>总金额</th>
                            <th>可用金额</th>
                            <th>最低金额</th>
                            <th>资金</th>
                        </tr>
                        </thead>
                        <tbody>
                        <% for (user in users) { %>
                        <tr>
                            <td>${user.id}</td>
                            <td>${user.nickname}</td>
                            <td>${user.phone}</td>
                            <td>${parseLong(user.asset.money / 100)}</td>
                            <td>${parseLong(user.asset.ableMoney / 100)}</td>
                            <td>${parseLong(user.asset.minMoney / 100)}</td>
                            <td><a href="${ctxPath}/admin/moneyRecords?userId=${user.id}">资金记录</a></td>
                        </tr>
                        <% } %>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-5">
            <div class="dataTables_info" id="example2_info" role="status" aria-live="polite">第${paging.page}页 共${paging.all}页 ${paging.count}条数据
            </div>
        </div>
        <div class="col-sm-7">
            <div class="dataTables_paginate paging_simple_numbers">
                <ul class="pagination">
                    <li class="paginate_button previous <% if (paging.page == 1) print('disabled'); %>">
                        <a href="${ctxPath}/admin/user?page=${paging.page - 1}" aria-controls="example2" data-dt-idx="0" tabindex="0">上一页</a>
                    </li>
                    <% for (thisPage in paging.shows) { %>
                    <li class="paginate_button <% if (thisPage == paging.page) print('active'); %>"><a href="${ctxPath}/admin/user?page=${thisPage}" aria-controls="example2" data-dt-idx="1" tabindex="0">${thisPage}</a></li>
                    <% } %>
                    <li class="paginate_button next <% if (paging.page == paging.all) print('disabled'); %>" id="example2_next">
                        <a href="${ctxPath}/admin/user?page=${paging.page + 1}" aria-controls="example2" data-dt-idx="7" tabindex="0">下一页</a></li>
                </ul>
            </div>
        </div>
    </div>
</div>
<script src="${ctxPath}/js/md5.min.js"></script>
<script src="${ctxPath}/js/net.js"></script>
<script>
    $(document).ready(function(){
        $('li.disabled a').attr('href', 'javascript:');
        $('#saveButton').click(function () {
            $('#createModal').modal('hide');
            var user = {
                nickname: $('#nicknameInput').val(),
                phone: $('#phoneInput').val(),
                password: md5($('#passwordInput').val())
            };
            var asset = {
                minMoney: parseInt($('#moneyInput').val()) * 100
            }
            net().url('${ctxPath}/api/users').data({user: user, asset: asset})
                .success(function () {
                    $.growl.notice({title: '保存成功', message: '用户保存成功' });
                })
                .error(function (err) {
                    $.growl.notice({title: '保存错误', message: '网络错误' });
                })
                .post();
        });
    });
</script>
<% } %>